package com.osnyun.sdk.utils;

import com.cloud.apigateway.sdk.utils.Request;
import com.osnyun.auth.vo.AuthorizationInfo;

import java.util.Map;

/**
 * 官网: https://support.huaweicloud.com/devg-apisign/api-sign-algorithm-002.html
 */
public class RequestHuiWeiWatewayUtils {

    /**
     * 获取华为云网关的token信息
     * @param key appKey
     * @param secret appSecret
     * @param method 请求方式
     * @param url 请求接口
     * @param p 入参 get请求不用传
     * @return
     * @throws Exception
     */
    public static AuthorizationInfo getAuthorizationInfo(String key, String secret, String method, String url, Map<String,Object> p) throws Exception {
        if("get".equals(method)){
            return requestGet(key,secret,method,url);
        }
        return null;
    }

    /**
     * get请求
     * @param key
     * @param secret
     * @param method
     * @param url
     */
    private static AuthorizationInfo requestGet(String key, String secret, String method, String url) throws Exception {
        Request request = new Request();
        try {
            request.setKey(key);
            request.setSecret(secret);
            request.setMethod(method);
            request.setUrl(url);
            request.addHeader("Content-Type", "text/plain");
            request.setBody("demo");
        } catch (Exception e) {
            e.printStackTrace();
        }
        //签名请求
        return Client.signOkhttp(request).getAuthorizationInfo();
    }
}
